#include "ssldemo.h"
#include <QFrame>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTextEdit>

#include "sslserver.h"
#include "sslclientthread.h"

/**
 * 參考：http://blog.csdn.net/winking_zhu/article/details/17508013
 *
 */
SSLDemo::SSLDemo(QWidget *parent) :
    QDialog(parent)
{
    resize(600,400);
    setWindowTitle("SSLDemo");
    QHBoxLayout * hboxlayout=new QHBoxLayout;
    QFrame* clientFrame=new QFrame;
    QTextEdit* clientEdit;
    QTextEdit* serverEdit;
    {
        QVBoxLayout* vboxlayout=new QVBoxLayout(clientFrame);
        QPushButton *pushbutton=new QPushButton;
        pushbutton->setText("Start Client");
        connect(pushbutton,SIGNAL(clicked()),this,SLOT(startClient()));
        vboxlayout->addWidget(pushbutton);

        clientEdit=new QTextEdit;
        vboxlayout->addWidget(clientEdit);

    }
    QFrame* serverFrame=new QFrame;
    {
        QVBoxLayout* vboxlayout=new QVBoxLayout(serverFrame);
        QPushButton *pushbutton=new QPushButton;
        pushbutton->setText("Start server");
        connect(pushbutton,SIGNAL(clicked()),this,SLOT(startServer()));
        vboxlayout->addWidget(pushbutton);

        serverEdit=new QTextEdit;
        vboxlayout->addWidget(serverEdit);

    }
    hboxlayout->addWidget(clientFrame);
    hboxlayout->addWidget(serverFrame);
    hboxlayout->setMargin(10);
    hboxlayout->setSpacing(5);
    setLayout(hboxlayout);

    mServerStarted=false;
}


void SSLDemo::startClient()
{
    SSLClientThread* thread=new SSLClientThread;
    thread->start();
}
static SSLServer* server=NULL;
void SSLDemo::startServer()
{
    mServerStarted=true;
    server=new SSLServer;
    server->startService();
}


void SSLDemo::stopClient()
{
    //wait the thread
}

void SSLDemo::stopServer()
{
    if(server)
        server->stopService();
    server=NULL;
}
